package au.com.iglootech.winerymap.ui.presenter.winerymanage;

import au.com.iglootech.component.IGTTfnField;
import au.com.iglootech.winerymap.resources.WineryMapStyle;

import com.vaadin.data.Item;
import com.vaadin.data.validator.EmailValidator;
import com.vaadin.data.validator.StringLengthValidator;
import com.vaadin.ui.Component;
import com.vaadin.ui.DefaultFieldFactory;
import com.vaadin.ui.Field;
import com.vaadin.ui.TextArea;
import com.vaadin.ui.TextField;

/**
 * 
 * @author Nicholas Zhu
 * 
 */
@org.springframework.stereotype.Component
public class WineryFieldFactory extends DefaultFieldFactory {
	/**
	 * 
	 */
	private static final long serialVersionUID = 6103154620745601868L;

	@Override
	public Field createField(final Item item, final Object propertyId, final Component uiContext) {
		Field f;
		if ("tfn".equals(propertyId)) {
			IGTTfnField tf = new IGTTfnField();
			tf.setCaption("TFN");
			tf.setWidth(WineryMapStyle.COMMON_FIELD_WIDTH);
			tf.addValidator(new StringLengthValidator("Last Name must be 3-50 characters", WineryMapStyle.MIN_TEXT_LEN, WineryMapStyle.MAX_TEXT_LEN,
					false));
			return tf;
		} else if ("description".equals(propertyId)) {
			TextArea ta = new TextArea();
			ta.setCaption("Description");
			ta.setNullRepresentation("");
			ta.setWidth(WineryMapStyle.COMMON_FIELD_WIDTH);
			ta.addValidator(new StringLengthValidator("Address must be 3-50 characters", WineryMapStyle.MIN_TEXT_LEN, 200, false));
			return ta;
		}
		// Use the super class to create a suitable field base on the
		// property type.
		f = super.createField(item, propertyId, uiContext);

		if ("companyName".equals(propertyId)) {
			TextField tf = (TextField) f;
			tf.setRequired(true);
			tf.setRequiredError("Please enter the company name");
			tf.setNullRepresentation("");
			tf.setWidth(WineryMapStyle.COMMON_FIELD_WIDTH);
			tf.addValidator(new StringLengthValidator("Company Name must be 3-100 characters", WineryMapStyle.MIN_TEXT_LEN, 100, false));
		} else if ("contactNumber".equals(propertyId)) {
			TextField tf = (TextField) f;
			tf.setRequired(true);
			tf.setRequiredError("Please enter a contact number");
			tf.setNullRepresentation("");
			tf.setWidth(WineryMapStyle.COMMON_FIELD_WIDTH);
			tf.addValidator(new StringLengthValidator("Contact Number must be 3-50 characters", WineryMapStyle.MIN_TEXT_LEN, 50, false));
		} else if ("contactName".equals(propertyId)) {
			TextField tf = (TextField) f;
			tf.setRequired(true);
			tf.setRequiredError("Please enter a contact name");
			tf.setNullRepresentation("");
			tf.setWidth(WineryMapStyle.COMMON_FIELD_WIDTH);
			tf.addValidator(new StringLengthValidator("Contact Name must be 3-50 characters", WineryMapStyle.MIN_TEXT_LEN, 50, false));
		} else if ("address1".equals(propertyId)) {
			TextField tf = (TextField) f;
			tf.setCaption("Address");
			tf.setRequired(true);
			tf.setRequiredError("Please enter the address");
			tf.setNullRepresentation("");
			tf.setWidth(WineryMapStyle.COMMON_FIELD_WIDTH);
			tf.addValidator(new StringLengthValidator("Address must be 3-50 characters", WineryMapStyle.MIN_TEXT_LEN, 50, false));
		} else if ("address2".equals(propertyId)) {
			TextField tf = (TextField) f;
			tf.setCaption("");
			tf.setRequired(true);
			tf.setRequiredError("Please enter the address");
			tf.setNullRepresentation("");
			tf.setWidth(WineryMapStyle.COMMON_FIELD_WIDTH);
			tf.addValidator(new StringLengthValidator("Address must be 3-50 characters", WineryMapStyle.MIN_TEXT_LEN, 50, false));
		} else if ("address3".equals(propertyId)) {
			TextField tf = (TextField) f;
			tf.setCaption("");
			tf.setRequired(true);
			tf.setRequiredError("Please enter the address");
			tf.setNullRepresentation("");
			tf.setWidth(WineryMapStyle.COMMON_FIELD_WIDTH);
			tf.addValidator(new StringLengthValidator("Address must be 3-50 characters", WineryMapStyle.MIN_TEXT_LEN, 50, false));
		} else if ("email".equals(propertyId)) {
			TextField tf = (TextField) f;
			tf.setRequired(true);
			tf.setRequiredError("Please enter a email");
			tf.setNullRepresentation("");
			tf.setWidth(WineryMapStyle.COMMON_FIELD_WIDTH);
			tf.addValidator(new EmailValidator("Your input is not validate email address."));
		}

		return f;
	}
}
